
function loadDiv(id, url, callback, data){
	if(url != undefined && url != null && url != ''){
		if(callback == undefined){
			if(data) {
				$('#'+id).load(url, data);
			} else {
				$('#'+id).load(url);
			}
		} else {
			$('#'+id).load(url, window[callback]);
		}
	}
}

function loadDataViaAjax(targetUrl, params, callback) {
   $.ajax({
   type:"GET",
   url:targetUrl,
   dataType:"json",  // this will a return a JSON either as object or array
   data: params,
   success: callback ,
   error:
	    function(xhr, desc, err) {
	        // currently shallow the error...probably enhance this with a callback as argument
	        // alert("Ajax call failed with the following response:\nDesc: " + desc + "\nErr:" + err );
	    } 
   });       
};

(function($){
	  $.fn.extend({ 
	    onShow: function(callback, unbind){
	      return this.each(function(){
	        var obj = this;
	        var bindopt = (unbind==undefined)?true:unbind; 
	        if($.isFunction(callback)){
	          if($(this).is(':hidden')){
	            var checkVis = function(){
	              if($(obj).is(':visible')){
	                callback.call();
	                if(bindopt){
	                  $('body').unbind('click keyup keydown', checkVis);
	                }
	              }                         
	            }
	            $('body').bind('click keyup keydown', checkVis);
	          }
	          else{
	            callback.call();
	          }
	        }
	      });
	    }
	  });
	})(jQuery);

jQuery.validator.setDefaults({
	onkeyup: false,
	errorClass: 'error',
	validClass: 'valid',
	errorPlacement: function(error, element)
	{
		// Set positioning based on the elements position in the form
		var elem = $(element).is("select:hidden") ? $(element).next() : $(element);
		
		var corners = ['left center', 'right center'];
		var flipIt = elem.parents('span.right').length > 0;

		// Check we have a valid error message
		if(!error.is(':empty')) {
			// Apply the tooltip only if it isn't valid
			elem.filter(':not(.valid)')
			.addClass('error')
			.qtip({
				overwrite: false,
				content: error,
				position: {
					my: corners[ flipIt ? 1 : 0 ],
					at: corners[ flipIt ? 0 : 1 ],
					viewport: $(window)
				},
				style: {
					classes: 'ui-tooltip-red' // Make it red... the classic error colour!
				}
			})

			// If we have a tooltip on this element already, just update its content
			.qtip('option', 'content.text', error);
		}

		// If the error is empty, remove the qTip
		else { elem.qtip('destroy'); }
	},
	success: $.noop // Odd workaround for errorPlacement not firing!
});